<?php

/**
 * 后台管理
 */
class AdminController extends Controller
{
    public $layout='//layouts/admin';
public $site_img_type=array(
//array('id'=>1,'name'=>'logo'),
array('id'=>2,'name'=>'轮循大图'),
array('id'=>3,'name'=>'产品小图'),
);
      //判断是否可以访问该模块
    /**
     * 过滤器
     */
    public function beforeAction($action)
    {
        if(!User::model()->user_verify())
            $this->redirectStay('/login/index','您无权访问');
        //$this->layout='/layouts/admin';
        return parent::beforeAction($action);
    }

    /**
     *网站基础信息配置
     */
    public function actionWebConfig(){
        if($_POST){
            $data=array(
                'time'=>time(),
                'web_name_cn'=>filterValue($_POST['form']['web_name_cn']),
                'web_name_en'=>filterValue($_POST['form']['web_name_en']),
                'logo_url'=>filterValue($_POST['form']['logo_url']),
            );
            ArrayToFile('WebConfig',$data);
        }
        $data=WebConfig::getData();
        $this->render('admin/web_config_form',array('data'=>$data));
    }


    /**
     *默认首页
     */
    public function actionIndex(){
                $this->redirect('/admin/Product');
        //$this->redirect('Product');
    }

    public function actionSite(){
        $this->left_list_on=11;
        $type=$this->site_img_type;
        $arr=array();
        if(is_array($type)){
            foreach($type as $k=>$v){
                $arr[$v['id']]=$v['name'];
            }
        }



        $sql="SELECT count(`id`) as `cnt` FROM `site_img_info`";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `site_img_info` $page->limit";
        $data=Yii::app()->db->createCommand($sql)->queryAll();

        $this->render('admin/site_img_list',array('type'=>$arr,'data'=>$data,'page_list'=>$page_list));
    }
    /**
     *产品新增及修改
     */
    public function actionSiteChange(){

        $this->left_list_on=11;
        $type_id=intval($_POST['type_id']);
        $img_file=$_FILES['imgFile'];


        if($type_id>0 && $img_file['size']>0){
            $file_url_head='images/site/';
            $tmp_file_url=$this->upload_img($img_file,$file_url_head.$type_id.'/');

            $product_type = new site_img_info();
            $product_type->type = $type_id;
            $product_type->img_url = $tmp_file_url;
            $product_type->time=time();
            $product_type->save();


            $this->redirectStay('/admin/site','图片上传成功');
        }


        $type=$this->site_img_type;
        $this->render('admin/site_img_form',array('type'=>$type,'data'=>$data,'imgs'=>$imgs));
    }

    /**
     *首页图ajax删除
     */
    public function actionSiteDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = site_img_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    public function actionImgShow(){
        $url=$_GET['url'];
        echo '<img src="'.$url.'" >;';
    }

    /**
     * 产品列表
     */
    public function actionProduct(){
        //$this->makeDir('imges/product/2/1');
        $this->left_list_on=2;
        $sql="SELECT count(*) as `cnt` FROM `product_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT *,
            `product_info`.`id` as `id`,`product_info`.`name_cn` as `product_name_cn`,`product_info`.`name_en` as `product_name_en`,
            `product_type`.`name_cn` as `type_name_cn`,`product_type`.`name_en` as `type_name_en`,
            `product_sub_type`.`name_cn` as `sub_type_name_cn`,`product_sub_type`.`name_en` as `sub_type_name_en`
 FROM `product_info`
 LEFT JOIN `product_sub_type` ON `product_info`.`type_id` = `product_sub_type`.`id`
 LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id` = `product_type`.`id`
 $where ORDER BY `product_info`.`id` DESC $page->limit
 ";
        $model=Yii::app()->db->createCommand($sql)->queryAll();


        $this->render('admin/product_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *产品新增及修改
     */
    public function actionProductChange(){

        $this->left_list_on=2;
        $form=$_POST['_form'];
        $img_files=$_FILES['imgFile'];


        if($form){
            $id=intval($_POST['id']);
            $file_url_head='images/product/';
            if($id>0){
                $del_imgs=$_POST['del_imgs'];
                $del_num=$this->DelImgs($del_imgs);


                $file_arr=array();
                for($i=0;$i<count($img_files['name']);$i++){
                    if(empty($img_files['name'][$i])){ continue;}
                    $file_arr[$i]=array('name'=>$img_files['name'][$i],'type'=>$img_files['type'][$i],'tmp_name'=>$img_files['tmp_name'][$i],'error'=>$img_files['error'][$i],'size'=>$img_files['size'][$i]);
                }
                if($img_files){
                    $error_img_i=0;$access_img_i=0;

                    for($i=0;$i<count($file_arr);$i++){
                        $product_img= new product_img_info();
                        $product_img->product_id=$id;

                        if($tmp_file_url=$this->upload_img($file_arr[$i],$file_url_head.intval($form['type_id']).'/')){
                            $product_img->url=$tmp_file_url;
                            $access_img_i++;
                        }else{
                            $error_img_i++;
                        }

                        $product_img->save();
                    }
                }

                $model=product_info::model()->findByPk($id);
                $model->attributes=$form;
                $model->img_count=$model->img_count-$del_num+$access_img_i;
                $model->update();
                //echo '修正'.$del_num.'-'.$access_img_i;
                //$this->redirect('type');
                $this->redirect('/admin/product');
            }else{
                $file_arr=array();
                for($i=0;$i<count($img_files['name']);$i++){
                    if(empty($img_files['name'][$i])){ continue;}
                    $file_arr[$i]=array('name'=>$img_files['name'][$i],'type'=>$img_files['type'][$i],'tmp_name'=>$img_files['tmp_name'][$i],'error'=>$img_files['error'][$i],'size'=>$img_files['size'][$i]);
                }

                $product_type = new product_info();
                $product_type->attributes = $form;
                $product_type->time=time();
                $product_type->save();
                $product_id=Yii::app()->db->getLastInsertID();
                if($img_files){
                    $error_img_i=0;$access_img_i=0;
                    for($i=0;$i<count($file_arr);$i++){
                        $product_img= new product_img_info();
                        $product_img->product_id=$product_id;
                        if($tmp_file_url=$this->upload_img($file_arr[$i],$file_url_head.intval($form['type_id']).'/')){
                            $product_img->url=$tmp_file_url;
                            $access_img_i++;
                        }else{
                            $error_img_i++;
                        }
                        $product_img->save();
                    }
                }
                $model=product_info::model()->findByPk($product_id);
                $model->img_count=$model->img_count+$access_img_i;
              //  echo $model->img_count;exit;
                $model->save();

                //echo '新增产品成功,图片上传成功'.$access_img_i.'失败'.$error_img_i.'个';
                //print_r($form['imgFile']);exit;
                $this->redirectStay('/admin/product','新增产品成功,图片上传成功'.$access_img_i.'失败'.$error_img_i.'个');
            }
        }
        //取得当前产品类型数组
        $type=product_sub_type::model()->findAll();
        $type=yiiObjectToArray($type);

        $id=intval($_GET['id']);
        if($id>0){
            $model=product_info::model()->findByPk($id);
            $data=$model->attributes;
            $model_imgs=product_img_info::model()->findAll('product_id=:product_id',array('product_id'=>$id));
            $imgs=yiiObjectToArray($model_imgs);

        }
        $this->render('admin/product_form',array('type'=>$type,'data'=>$data,'imgs'=>$imgs));
    }
    /**
     * 删除图片
     */
    public function DelImgs($arr=array()){
        $i=0;
        if(is_array($arr)){
            foreach( $arr as $k =>$v){
                if(intval($v)==0){continue;}
                $img_model=product_img_info::model()->findByPk(intval($v));
//                if(file_exists(Yii::app()->basePath.'/../'.$img_model->url)){
//                  //  unlink(Yii::app()->basePath.'/../'.$img_model->url); //权限问题无法删除
//                }
                $img_model->delete();
                $i++;
            }
        }
        return $i;
    }
    /**
     *产品ajax删除
     */
    public function actionProductDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = product_info::model()->findByPk($id);
            $img_model=product_img_info::model()->findAll('product_id=:id',array(':id'=>$id));
            $img_arr=yiiObjectToArray($img_model);
            foreach($img_arr as $k=>$v){
                if(file_exists(Yii::app()->basePath.'/../'.$v['url'])){
                    if(!empty($v['url'])){
                        unlink(Yii::app()->basePath.'/../'.$v['url']);
                    }
                }
            }
            $img_model=product_img_info::model()->DeleteAll('product_id=:id',array(':id'=>$id));
            $model->delete();
            product_img_info::model()->deleteAll("`product_id`=:id",array(':id'=>$id));
        }
        echo 'ok';
    }

    /**
     *产品类型列表
     */
    public function actionType(){
        $this->left_list_on=3;
        $sql="SELECT count(*) as `cnt` FROM `product_type` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `product_type` $where ORDER BY id DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/type_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *产品类型新增及修改
     */
    public function actionTypeChange(){
        $this->left_list_on=3;
        $form=$_POST['_form'];
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=product_type::model()->findByPk($id);
                $model->attributes=$form;
                $model->update();
                $this->redirect('/admin/type');
            }else{
                $product_type = new product_type();
                $product_type->attributes = $form;
                $product_type->save();
                $this->redirect('/admintype');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model=product_type::model()->findByPk($id);
            $data=$model->attributes;
        }
        $this->render('admin/type_form',array('data'=>$data));
    }
    /**
     *产品类型ajax删除
     */
    public function actionTypeDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = product_type::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *产品子类型列表
     */
    public function actionSubType(){
        $this->left_list_on=9;
        $sql="SELECT count(*) as `cnt` FROM `product_sub_type` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT `product_type`.`name_cn` as `name_cn`,`product_type`.`name_en` as `name_en`,
            `product_sub_type`.`name_cn` as `sub_name_cn`,`product_sub_type`.`name_en` as `sub_name_en`,`product_sub_type`.`id` as `id` FROM `product_sub_type`
            LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id` = `product_type`.`id`
            $where ORDER BY `product_sub_type`.`id` DESC
            $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/type_sub_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *产品子类型新增及修改
     */
    public function actionSubTypeChange(){
        $this->left_list_on=9;
        $form=$_POST['_form'];

        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=product_sub_type::model()->findByPk($id);
                $model->attributes=$form;
                $model->update();
                $this->redirect('/admin/subType');
            }else{
                $product_type = new product_sub_type();
                $product_type->attributes = $form;
                $product_type->save();
                $this->redirect('/admin/subType');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model=product_sub_type::model()->findByPk($id);
            $data=$model->attributes;
        }
        //取得当前产品类型数组
        $type=product_type::model()->findAll();
        $type=yiiObjectToArray($type);

        $this->render('admin/sub_type_form',array('data'=>$data,'type'=>$type));
    }
    /**
     *产品子类型ajax删除
     */
    public function actionSubTypeDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = product_type::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *新闻类型列表
     */
    public function actionNewsType(){
        $this->left_list_on=4;
        $sql="SELECT count(*) as `cnt` FROM `news_type` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `news_type` $where ORDER BY id DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/news_type_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *新闻类型新增及修改
     */
    public function actionNewsTypeChange(){
        $this->left_list_on=4;
        $form=$_POST['_form'];
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=news_type::model()->findByPk($id);
                $model->attributes=$form;
                $model->update();
                $this->redirect('/admin/newsType');
            }else{
                $product_type = new news_type();
                $product_type->attributes = $form;
                $product_type->save();
                $this->redirect('/admin/newsType');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model=news_type::model()->findByPk($id);
            $data=$model->attributes;
        }
        $this->render('admin/news_type_form',array('data'=>$data));
    }
    /**
     *新闻类型ajax删除
     */
    public function actionNewsTypeDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = news_type::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *新闻列表
     */
    public function actionNews(){
        $this->left_list_on=5;
        $sql="SELECT count(*) as `cnt` FROM `news_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT *,`news_info`.`id` as `id` FROM `news_info` left JOIN `news_type` ON `news_info`.`type_id`=`news_type`.`id` $where ORDER BY `news_info`.`id` DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/news_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *新闻新增及修改
     */
    public function actionNewsChange(){
        $this->left_list_on=5;
        $form=$_POST['_form'];
        $file_url_head='images/news/';
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=news_info::model()->findByPk($id);
                $model->attributes=$form;
                $model->time=time();
                if($tmp_file_url=$this->upload_img($_FILES['imgFile'],$file_url_head)){
                    $model->img_url=$tmp_file_url;
                }
                $model->update();
                $this->redirect('/admin/news');
            }else{
                $model = new news_info();
                $model->attributes = $form;
                $model->time=time();
                if($tmp_file_url=$this->upload_img($_FILES['imgFile'],$file_url_head)){
                    $model->img_url=$tmp_file_url;
                }
                $model->save();
                $this->redirect('/admin/news');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model=news_info::model()->findByPk($id);
            $data=$model->attributes;
        }

        //取得当前新闻类型数组
        $type=news_type::model()->findAll();
        $type=yiiObjectToArray($type);
        $this->render('admin/news_form',array('data'=>$data,'type'=>$type));
    }
    /**
     *产品类型ajax删除
     */
    public function actionNewsDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = news_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    //在线编辑器
    public function actionEditor()
    {
        echo $_GET['myEditor'];
        $this->render('editor');
    }

    /**
     *关于我们列表
     */
    public function actionAboutUs(){
        $this->left_list_on=6;
        $sql="SELECT count(*) as `cnt` FROM `about_us_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `about_us_info`  $where ORDER BY `about_us_info`.`id` DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/about_us_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *关于我们新增及修改
     */
    public function actionAboutUsChange(){
        $this->left_list_on=6;
        $form=$_POST['_form'];
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=about_us_info::model()->findByPk($id);
                $model->attributes=$form;
                $model->time=time();
                $model->update();
                $this->redirect('/admin/aboutUs');
            }else{
                $product_type = new about_us_info();
                $product_type->attributes = $form;
                $product_type->time=time();
                $product_type->save();
                $this->redirect('/admin/aboutUs');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model = about_us_info::model()->findByPk($id);
            $data = $model->attributes;
        }
        $this->render('admin/about_us_form',array('data'=>$data,'type'=>$type));
    }
    /**
     *关于我们ajax删除
     */
    public function actionAboutUsDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = about_us_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *服务中心列表
     */
    public function actionServer(){
        $this->left_list_on=7;
        $sql="SELECT count(*) as `cnt` FROM `server_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `server_info`  $where ORDER BY `server_info`.`id` DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/server_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *服务中心新增及修改
     */
    public function actionServerChange(){
        $this->left_list_on=7;
        $form=$_POST['_form'];
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=server_info::model()->findByPk($id);
                $model->attributes=$form;
                $model->time=time();
                $model->update();
                $this->redirect('/admin/server');
            }else{
                $product_type = new server_info();
                $product_type->attributes = $form;
                $product_type->time=time();
                $product_type->save();
                $this->redirect('/admin/server');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model = server_info::model()->findByPk($id);
            $data = $model->attributes;
        }
        $this->render('admin/server_form',array('data'=>$data,'type'=>$type));
    }
    /**
     *服务中心ajax删除
     */
    public function actionServerDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = server_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *联系我们列表
     */
    public function actionContactUs(){
        $this->left_list_on=8;
        $sql="SELECT count(*) as `cnt` FROM `contact_us_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `contact_us_info`  $where ORDER BY `contact_us_info`.`id` DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/contact_us_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *联系我们新增及修改
     */
    public function actionContactUsChange(){
        $this->left_list_on=8;
        $form=$_POST['_form'];
        if($form){
            $id=intval($_POST['id']);
            if($id>0){
                $model=contact_us_info::model()->findByPk($id);
                $model->attributes=$form;
                $model->time=time();
                $model->update();
                $this->redirect('/admin/server');
            }else{
                $product_type = new contact_us_info();
                $product_type->attributes = $form;
                $product_type->time=time();
                $product_type->save();
                $this->redirect('/admin/contactUs');
            }
        }
        $id=intval($_GET['id']);
        if($id>0){
            $model = contact_us_info::model()->findByPk($id);
            $data = $model->attributes;
        }
        $this->render('admin/contact_us_form',array('data'=>$data,'type'=>$type));
    }
    /**
     *联系我们ajax删除
     */
    public function actionContactUsDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = contact_us_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    /**
     *留言列表
     */
    public function actionMessage(){
        $this->left_list_on=10;
        $sql="SELECT count(*) as `cnt` FROM `message_info` $where $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $cnt=$model[0]['cnt'];
        $page = new Pagination($cnt,$this->page_num);
        $page_list = $page->fpage(array(0,2,3,4,5,6,7,8));
        $sql="SELECT * FROM `message_info`  $where ORDER BY `message_info`.`id` DESC $page->limit";
        $model=Yii::app()->db->createCommand($sql)->queryAll();
        $this->render('admin/message_list',array('data'=>$model,'page_list'=>$page_list));
    }
    /**
     *留言ajax删除
     */
    public function actionMessageDel(){
        $id=intval($_POST['id']);
        if($id>0){
            $model = message_info::model()->findByPk($id);
            $model->delete();
        }
        echo 'ok';
    }

    public function actionSeo(){
       // $seoData=new seoData;
    }


}